package cn.lyjuan.pojoutils.bean;

import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.pojoutils.cst.BaseCst;
import cn.lyjuan.pojoutils.util.PojoUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ly on 2014/11/20.
 */
public class TableBean
{
    /**
     * POJO包名
     */
    private String pojoPkgName;

    /**
     * Mapper接口和 Mapper XML 文件包名，如果没有设置则为 POJO 包名将 pojo 替换为 mapper
     */
    private String mapperPkgName;

    /**
     * 表名
     */
    private String tableName;

    /**
     * POJO 名
     */
    private String pojoName;

    /**
     * 表注释
     */
    private String pojoComment;

    /**
     * Mapper接口和 Mapper XML 文件名，如果没有设置则为 pojoName 前缀加I（表示接口），后缀Pojo替换为Mapper
     * 如：UserPojo 对应 Mapper 为 IUserMapper
     */
    private String mapperName;

    /**
     * POJO的资源名（默认 src）
     */
    private String pojoSrcName = "src";

    /**
     * Mapper 接口资源名（默认 src）
     */
    private String mapperSrcName = "src";

    /**
     * Mapper的资源名(默认src)
     */
    private String mapperXmlSrcName = "src";

    /**
     * 序列(Oracle)
     */
    private Map<String, String> seqs;

    /**
     * POJO对象名，如UserPojo 的对象名为 user; UserClassPojo 对象名为 userClass
     */
    private String objName;

    /**
     * 数据库驱动
     */
    private String driver;

    /**
     * 数据库
     */
    private String database;

    /**
     * 数据库链接URL
     */
    private String url;

    /**
     * 数据库用户名
     */
    private String user;

    /**
     * 数据库密码
     */
    private String pwd;

    /**
     * 运行方式，默认 {@code RunType.PRINT_ALL}
     */
    private RunType runType = RunType.PRINT_ALL;

    /**
     * 在 Mapper XML 文件中，判断是否更新 int 类值，当值为指定值时，不更新内容
     */
    private String numberInvalidVal = BaseCst.DEF_NUMBER_INVALID_VAL;

    /**
     * Getter and Setter Methods *
     */
    public String getPojoPkgName()
    {
        return pojoPkgName;
    }

    public void setPojoPkgName(String pojoPkgName)
    {
        if (StringUtils.isNull(pojoPkgName))
            throw new RuntimeException("pojoPkgName can not empty");
        this.pojoPkgName = pojoPkgName;

        if (StringUtils.isNull(this.mapperPkgName))// 设置 Mapper 包名为默认
            this.mapperPkgName = pojoPkgName.replace(BaseCst.SUF_POJO.toLowerCase(), BaseCst.SUF_MAPPER.toLowerCase());
    }

    public String getMapperPkgName()
    {
        return mapperPkgName;
    }

    public void setMapperPkgName(String mapperPkgName)
    {
        if (StringUtils.isNull(mapperPkgName))
            throw new RuntimeException("mapperPkgName can not empty");
        this.mapperPkgName = mapperPkgName;
    }

    public String getTableName()
    {
        return tableName;
    }

    public void setTableName(String tableName)
    {
        if (StringUtils.isNull(tableName))
            throw new RuntimeException("tableName can not empty");
        this.tableName = tableName;
    }

    public String getPojoName()
    {
        return pojoName;
    }

    public void setPojoName(String pojoName)
    {
        if (StringUtils.isNull(pojoName))
            throw new RuntimeException("pojoName can not empty");
        if (!StringUtils.isNull(this.objName))
            this.pojoName = pojoName.replace(BaseCst.RP_NAME, PojoUtils.firstUpLowCase(this.getObjName(), true));
        else
            this.pojoName = pojoName;
    }

    public String getMapperName()
    {
        return mapperName;
    }

    public void setMapperName(String mapperName)
    {
        if (StringUtils.isNull(mapperName))
            throw new RuntimeException("mapperName can not empty");
        if (!StringUtils.isNull(this.objName))
            this.mapperName = mapperName.replace(BaseCst.RP_NAME, PojoUtils.firstUpLowCase(objName, true));
        else
            this.mapperName = mapperName;
    }

    public String getPojoSrcName()
    {
        return pojoSrcName;
    }

    public void setPojoSrcName(String pojoSrcName)
    {
        if (StringUtils.isNull(pojoSrcName))
            throw new RuntimeException("pojoSrcName can not empty");
        this.pojoSrcName = pojoSrcName;
    }

    public String getMapperXmlSrcName()
    {
        return mapperXmlSrcName;
    }

    public void setMapperXmlSrcName(String mapperXmlSrcName)
    {
        if (StringUtils.isNull(mapperXmlSrcName))
            throw new RuntimeException("mapperXmlSrcName can not empty");
        this.mapperXmlSrcName = mapperXmlSrcName;
    }

    public String getMapperSrcName()
    {
        return mapperSrcName;
    }

    public void setMapperSrcName(String mapperSrcName)
    {
        if (StringUtils.isNull(mapperSrcName))
            throw new RuntimeException("mapperSrcName can not empty");
        this.mapperSrcName = mapperSrcName;
    }

    public Map<String, String> getSeqs()
    {
        return seqs;
    }

    public void setSeqs(Map<String, String> seqs)
    {
        this.seqs = seqs;
    }

    public String putSeq(String column, String seqName)
    {
        if (null == this.seqs)
            this.seqs = new HashMap<String, String>();
        return this.seqs.put(column, seqName);
    }

    public String getSeq(String column)
    {
        if (null == this.seqs)
            return null;
        return this.seqs.get(column);
    }

    public String getPojoComment() {return pojoComment;}
    public void setPojoComment(String pojoComment) {this.pojoComment = pojoComment;}
    public String getObjName() {return objName;}
    public void setObjName(String objName)
    {
        if (StringUtils.isNull(objName))
            throw new IllegalArgumentException("objName can not empty");

        this.objName = objName;

        if (!StringUtils.isNull(pojoName))
            this.setPojoName(this.getPojoName().replace(BaseCst.RP_NAME, PojoUtils.firstUpLowCase(objName, true)));
        else // 默认名称
            this.setPojoName(PojoUtils.firstUpLowCase(objName, true) + BaseCst.SUF_POJO);
        if (!StringUtils.isNull(mapperName))
            this.setMapperName(this.getMapperName().replace(BaseCst.RP_NAME, PojoUtils.firstUpLowCase(objName, true)));
        else // 默认名称
            this.setMapperName("I" + PojoUtils.firstUpLowCase(objName, true) + BaseCst.SUF_MAPPER);
    }

    public String getDriver() {return driver;}
    public void setDriver(String driver) {this.driver = driver;}
    public String getUrl() {return url;}
    public void setUrl(String url)
    {
        if (StringUtils.isNull(url))
            throw new IllegalArgumentException("url can not empty");

        if (StringUtils.isNull(this.database))
        {
            String db = url.substring(url.lastIndexOf("/") + 1);

            if (db.indexOf("?") > -1)
                db = db.substring(0, db.indexOf("?"));

            this.database = db;
        }

        this.url = url;
    }
    public String getUser() {return user;}
    public void setUser(String user) {this.user = user;}
    public String getPwd() {return pwd;}
    public void setPwd(String pwd) {this.pwd = pwd;}
    public String getDatabase() { return database; }
    public void setDatabase(String database) { this.database = database; }
    public RunType getRunType() { return runType; }
    public void setRunType(RunType runType) { this.runType = runType; }

    public String getNumberInvalidVal() {return numberInvalidVal;}
    public void setNumberInvalidVal(String numberInvalidVal)
    {
        if (!StringUtils.isNumber(numberInvalidVal))
            throw new IllegalArgumentException(numberInvalidVal + " not a number value");
        this.numberInvalidVal = numberInvalidVal;
    }

    public TableBean(String pojoPkgName, String mapperPkgName, String tableName, String pojoName, String mapperName,
                     String pojoSrcName, String mapperSrcName, String mapperXmlSrcName, Map<String, String> seqs)
    {
        setPojoPkgName(pojoPkgName);
        setMapperPkgName(mapperPkgName);
        setTableName(tableName);
        setPojoName(pojoName);
        setMapperName(mapperName);
        setPojoSrcName(pojoSrcName);
        setMapperSrcName(mapperSrcName);
        setMapperXmlSrcName(mapperXmlSrcName);
        setSeqs(seqs);
    }

    public TableBean() { }
}
